import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/widgets.dart' hide Page, Action;
import 'package:hc_center/basic/state.dart';
import 'package:hc_center/basic/store.dart';
import 'package:hc_center/pages/main/main_page.dart';
import 'package:hc_center/pages/map/map_page.dart';
import 'package:hc_center/pages/route/route_page.dart';
import 'package:hc_center/pages/rtmp/rtmp_page.dart';
import 'package:hc_center/pages/start/start_page.dart';
abstract class RoutesName{
  static const startPage = 'startPage'; /// 启动
  static const mainPage = 'mainPage'; /// 首页
  static const mapPage = 'mapPage'; /// 首页
  static const rtmpPage = 'rtmpPage'; /// 首页
  static const routePage = 'routePage'; /// 首页
}
class Routes {
  static final PageRoutes routes = PageRoutes(
    pages: <String, Page<Object, dynamic>>{
      RoutesName.startPage: StartPage(),
      RoutesName.mainPage: MainPage(),
      RoutesName.mapPage: MapPage(),
      RoutesName.rtmpPage: RtmpPage(),
      RoutesName.routePage: RoutePage(),
    },
    visitor: (String path, Page<Object, dynamic> page) {
      /// 只有特定的范围的 Page 才需要建立和 AppStore 的连接关系
      /// 满足 Page<T> ，T 是 AppGlobalBaseState 的子类
      if (page.isTypeof<AppGlobalBaseState>()) {
        /// 建立 AppStore 驱动 PageStore 的单向数据连接
        /// 1. 参数1 AppStore
        /// 2. 参数2 当 AppStore.state 变化时, PageStore.state 该如何变化
        page.connectExtraStore<AppGlobalState>(AppGlobalStore.store,
                (Object pagestate, AppGlobalState appState) {
              final AppGlobalBaseState p = pagestate;
              if (p.themeColorIndex != appState.themeColorIndex ||
                  p.locale != appState.locale ) {
                if (pagestate is Cloneable) {
                  debugPrint('修改--进行复制');
                  final Object copy = pagestate.clone();
                  final AppGlobalBaseState newState = copy;
                  newState.themeColorIndex = appState.themeColorIndex;
                  newState.locale = appState.locale;
                  return newState;
                }
              }
              return pagestate;
            });
      }
      /// AOP
      /// 页面可以有一些私有的 AOP 的增强， 但往往会有一些 AOP 是整个应用下，所有页面都会有的。
      /// 这些公共的通用 AOP ，通过遍历路由页面的形式统一加入。
      page.enhancer.append(
        /// View AOP
        viewMiddleware: <ViewMiddleware<dynamic>>[
          safetyView<dynamic>(),
        ],

        /// Adapter AOP
        adapterMiddleware: <AdapterMiddleware<dynamic>>[
          safetyAdapter<dynamic>()
        ],

        /// Effect AOP
        effectMiddleware: [
          _pageAnalyticsMiddleware<dynamic>(),
        ],

        /// Store AOP
        middleware: <Middleware<dynamic>>[
          logMiddleware<dynamic>(tag: page.runtimeType.toString()),
        ],
      );
    },
  );
}

EffectMiddleware<T> _pageAnalyticsMiddleware<T>() {
  return (AbstractLogic<dynamic> logic, Store<T> store) {
    return (Effect<dynamic> effect) {
      return (Action action, Context<dynamic> ctx) {
        if (logic is Page<dynamic, dynamic> && action.type is Lifecycle) {
          debugPrint('${logic.runtimeType} ${action.type.toString()} ');
        }
        return effect?.call(action, ctx);
      };
    };
  };
}
